<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="父id" prop="pid">
        <el-input v-model="dataForm.pid" placeholder="父id"></el-input>
      </el-form-item>
          <el-form-item label="简称" prop="shortname">
        <el-input v-model="dataForm.shortname" placeholder="简称"></el-input>
      </el-form-item>
          <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
          <el-form-item label="全称" prop="mergerName">
        <el-input v-model="dataForm.mergerName" placeholder="全称"></el-input>
      </el-form-item>
          <el-form-item label="层级 1 2 3 省市区县" prop="level">
        <el-input v-model="dataForm.level" placeholder="层级 1 2 3 省市区县"></el-input>
      </el-form-item>
          <el-form-item label="拼音" prop="pinyin">
        <el-input v-model="dataForm.pinyin" placeholder="拼音"></el-input>
      </el-form-item>
          <el-form-item label="长途区号" prop="code">
        <el-input v-model="dataForm.code" placeholder="长途区号"></el-input>
      </el-form-item>
          <el-form-item label="邮编" prop="zipCode">
        <el-input v-model="dataForm.zipCode" placeholder="邮编"></el-input>
      </el-form-item>
          <el-form-item label="首字母" prop="first">
        <el-input v-model="dataForm.first" placeholder="首字母"></el-input>
      </el-form-item>
          <el-form-item label="经度" prop="lng">
        <el-input v-model="dataForm.lng" placeholder="经度"></el-input>
      </el-form-item>
          <el-form-item label="纬度" prop="lat">
        <el-input v-model="dataForm.lat" placeholder="纬度"></el-input>
      </el-form-item>
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '',  pid: '',  shortname: '',  name: '',  mergerName: '',  level: '',  pinyin: '',  code: '',  zipCode: '',  first: '',  lng: '',  lat: ''});

const rules = ref({
          pid: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          shortname: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          name: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          mergerName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          level: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          pinyin: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          code: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          zipCode: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          first: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          lng: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          lat: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/sysregion/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/api/sysregion", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
